package cn.tedu.store.order.webapi.controller;

import cn.tedu.store.commons.pojo.order.dto.CartAddNewDTO;
import cn.tedu.store.commons.pojo.order.dto.CartUpdateDTO;
import cn.tedu.store.commons.pojo.order.vo.CartStandardVO;
import cn.tedu.store.commons.web.JsonPage;
import cn.tedu.store.commons.web.JsonResult;
import cn.tedu.store.order.service.ICartService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "购物车管理模块")
@RestController
@RequestMapping("/oms/cart")
public class CartController {

    @Autowired
    private ICartService cartService;

    @ApiOperation("新增购物车")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('user')")
    public JsonResult<Void> addCart(@Validated CartAddNewDTO cartAddNewDTO){
        log.debug("处理[新增购物车]请求 参数:{}",cartAddNewDTO);
        cartService.addCart(cartAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据userId分页查询购物车")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", example = "5")
    })
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('user')")
    public JsonResult<JsonPage<CartStandardVO>> addCart(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize){
        log.debug("处理[查询购物车]请求 参数:{},{}", pageNum, pageSize);
        return JsonResult.ok(cartService.listCarts(pageNum, pageSize));
    }

    @ApiOperation("根据id删除购物车中的sku信息")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "ids", value = "id数组", required = true, dataType = "array")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('user')")
    public JsonResult<Void> deleteByIds(Long[] ids){
        cartService.removeCart(ids);
        return JsonResult.ok();
    }

    @ApiOperation("清空购物车")
    @ApiOperationSupport(order = 210)
    @PostMapping("/delete/all")
    @PreAuthorize("hasAuthority('user')")
    public JsonResult<String> deleteAll(){
        cartService.removeAllCarts();
        return JsonResult.ok("购物车已清空");
    }

    @ApiOperation("修改购物车中sku数量")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update/quantity")
    @PreAuthorize("hasAuthority('user')")
    public JsonResult<Void> updateQuantity(@Validated CartUpdateDTO cartUpdateDTO){
        cartService.updateQuantity(cartUpdateDTO);
        return JsonResult.ok();
    }


}